package suanfa;

import java.util.Arrays;

public class RandomTest5 {
    public static void main(String[] args) {
        int times = 10000;
        for (int i = 0; i < times; i++) {
            int arr[] = createRandomArr(100, 10);
            int[] copyArr = copyArr(arr);
            BubbleSortTest.sort(arr);
            if (!isSorted(arr)) {
                System.out.println("排序前：" + Arrays.toString(copyArr));
                System.out.println("排序失败");
                System.out.println("排序后：" + Arrays.toString(arr));
                break;
            }
        }
    }

    private static int[] copyArr(int[] arr) {
        int[] copyArr = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            copyArr[i] = arr[i];
        }
        return copyArr;
    }

    public static int[] createRandomArr(int maxVal, int maxLen) {
        int len = (int) (Math.random() * maxLen);
        int[] arr = new int[len];
        for (int i = 0; i < len; i++) {
            arr[i] = (int) (Math.random() * maxVal);
        }
        return arr;
    }

    private static boolean isSorted(int[] arr) {
        if (arr.length < 2) {
            return true;
        }
        int min = arr[0];
        for (int i : arr) {
            if (min > i) {
                return false;
            }
            min = i;
        }
        return true;
    }
}
